/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.event.TreeModelEvent;
import org.aswing.event.TreeModelListener;
import org.aswing.tree.MutableTreeNode;
import org.aswing.tree.TreeModel;
import org.aswing.tree.TreeNode;
import org.aswing.tree.TreePath;
import org.aswing.util.ArrayUtils;

import fever.exception.IllegalArgumentException;
import fever.utils.Stringifier;

import fvaswing.components.tree.FvAbstractTreeNode;

/**
 * Default model to use with {@link fvaswing.components.FvTree} component.
 * 
 * <p>Implements {@link fvaswing.components.FvTreeModel} interface.
 * 
 * <p>Based on on <strong>AsWing DefaultTreeModel</strong> implementation.
 * 
 * @author Romain Ecarnot
 */
class fvaswing.components.tree.FvAbstractTreeModel implements TreeModel
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var TREE_STRUCTURE_CHANGED_HANDLER : String = 'treeStructureChanged';	private static var TREE_NODES_CHANGED_HANDLER : String = 'treeNodesChanged';	private static var TREE_NODES_INSERTED_HANDLER : String = 'treeNodesInserted';	private static var TREE_NODES_REMOVED_HANDLER : String = 'treeNodesRemoved';
	
    private var _rootNode : TreeNode;
    private var _listenerList : Array;
	private var _asksAllowsChildren : Boolean;
    
    private var _autoSortingEnabled : Boolean;
	private var _sortingProcess : Function;
	private var _sortingOptions : Number;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	 
	/**
	 * Defines sorting process to use when childnodes are automatically reorder.
	 * 
	 * @param process Sorting function
	 * @param options Sorting options
	 */
	public function setSortingProcess( process : Function, options : Number ) : Void
	{
		if( process ) _sortingProcess = process;
		if( options > 1 ) _sortingOptions = options; 
	}
	
    /**
      * Sets whether or not to test leafness by asking getAllowsChildren()
      * or isLeaf() to the TreeNodes.  If newvalue is true, getAllowsChildren()
      * is messaged, otherwise isLeaf() is messaged.
      */
    public function setAsksAllowsChildren( newValue : Boolean ) : Void
    {
        _asksAllowsChildren = newValue;
    }

    /**
      * Tells how leaf nodes are determined.
      *
      * @return true if only nodes which do not allow children are
      *         leaf nodes, false if nodes which have no children
      *         (even if allowed) are leaf nodes
      */
    public function asksAllowsChildren() : Boolean
    {
        return _asksAllowsChildren;
    }

    /**
     * Sets the root to {@code root}.<br />
     * A null <code>root</code> implies the tree is to display 
     * nothing, and is legal.
     */
    public function setRoot( root : TreeNode ) : Void
    {
        var oldRoot : Object = _root;
        
		_rootNode = root;
		
        if ( root == null && oldRoot != null )
        {
            _fireTreeStructureChanged( this, null );
            
        }
        else
        {
            nodeStructureChanged( root );
        }
    }

    /**
     * Returns the root of the tree.  Returns null only if the tree has
     * no nodes.
     *
     * @return  the root of the tree
     */
    public function getRoot() : Object
    {
        return _rootNode;
    }

    /**
     * Returns the index of child in parent.
     * If either the parent or child is <code>null</code>, returns -1.
     * 
     * @param parent a note in the tree, obtained from this data source
     * @param child the node we are interested in
     * @return the index of the child in the parent, or -1
     *    if either the parent or the child is <code>null</code>
     */
    public function getIndexOfChild( parent : Object, child : Object ) : Number
    {
        if( parent == null || child == null ) return -1;
        return parent.getIndex( child );
    }
	
    /**
     * Returns the child of <I>parent</I> at index <I>index</I> in the parent's
     * child array.  <I>parent</I> must be a node previously obtained from
     * this data source. This should not return null if <i>index</i>
     * is a valid index for <i>parent</i> (that is <i>index</i> >= 0 &&
     * <i>index</i> < getChildCount(<i>parent</i>)).
     *
     * @param   parent  a node in the tree, obtained from this data source
     * @return  the child of <I>parent</I> at index <I>index</I>
     */
    public function getChild( parent : Object, index : Number ) : Object
    {
        return parent.getChildAt( index );
    }
	
    /**
     * Returns the number of children of <I>parent</I>.  Returns 0 if the node
     * is a leaf or if it has no children.  <I>parent</I> must be a node
     * previously obtained from this data source.
     *
     * @param   parent  a node in the tree, obtained from this data source
     * @return  the number of children of the node <I>parent</I>
     */
    public function getChildCount( parent : Object ) : Number
    {
        return parent.getChildCount();
    }

    /** 
     * Returns whether the specified node is a leaf node.
     * 
     * @param node the node to check
     * @return <code>true</code> if the node is a leaf node
     */
    public function isLeaf( node : Object ) : Boolean
    {
        if( asksAllowsChildren() ) 
        {
            return !node.getAllowsChildren();
        }
        return node.isLeaf();
    }

    /**
      * This sets the user object of the TreeNode identified by path
      * and posts a node changed.  
      * 
      * <p>If you use custom user objects in the TreeModel you're going to 
      * need to subclass this and set the user object of the changed node 
      * to something meaningful.
      */
    public function valueForPathChanged( path : TreePath, newValue : Object ) : Void
    {
		var aNode : MutableTreeNode = MutableTreeNode( path.getLastPathComponent() );
		aNode.setUserObject( newValue );
		
        nodeChanged( aNode );
    }

    /**
     * Invoked this to insert newChild at location index in parents children.
     * 
     * <p>This will then message nodesWereInserted to create the appropriate
     * event. 
     * 
     * <p>This is the preferred way to add children as it will create
     * the appropriate event.
     */
    public function insertNodeInto( newChild : MutableTreeNode, parent : MutableTreeNode, index : Number ) : Void
    {
        parent.insert( newChild, index );
		
        nodesWereInserted( parent, [index] );
    }
	
    /**
     * Message this to remove node from its parent. This will message
     * nodesWereRemoved to create the appropriate event. This is the
     * preferred way to remove a node as it handles the event creation
     * for you.
     */
    public function removeNodeFromParent( node : MutableTreeNode ) : Void
    {
        var parent : MutableTreeNode = MutableTreeNode( node.getParent() );
		
        if( parent == null ) 
        {
        	throw new IllegalArgumentException( 'node [ ' + node + ' ] does not have parent' );
        }

        var childIndex : Array = [ parent.getIndex( node ) ];
        parent.removeAt( childIndex[0] );
        
        var removedArray : Array = [ node ];
        nodesWereRemoved( parent, childIndex, removedArray );
    }

    /**
      * Invoke this method after you've changed how node is to be
      * represented in the tree.
      */
    public function nodeChanged( node : TreeNode ) : Void
    {
        if( _listenerList != null && node != null )
        {
            var parent : TreeNode = node.getParent();
            
            if( parent != null )
            {
                var anIndex : Number = parent.getIndex( node );
                
                if( anIndex != -1 ) nodesChanged(parent, [anIndex]);
                
            }
            else if ( node == getRoot() )
            {
				nodesChanged( node, null );
	    	}
        }
    }
	
    /**
     * Invoke this method if you've modified the TreeNodes upon which this
     * model depends.  The model will notify all of its listeners that the
     * model has changed below the node <code>node</code> (PENDING).
     * @param node (optional). Default is root.
     */
    public function reload( node : TreeNode ) : Void
    {
    	if( node == undefined ) node = _rootNode;
    	
        if(node != null) _fireTreeNodeEvent( this, _getPathToRoot( node ), null, null, TREE_STRUCTURE_CHANGED_HANDLER );
    }
	
	/**
	 * Sets if childnodes are automatically reorder ( sort ). 
	 */
	public function setAutoSortingEnabled( b : Boolean ) : Void
	{
		_autoSortingEnabled = b;	
	}
	
	/**
	 * Returns <code>true</code> if auto sorting is enabled.
	 */
	public function getAutoSortingEnabled() : Boolean
	{
		return _autoSortingEnabled;	
	}
	
	/**
	 * Sorts full tree childnodes.
	 * 
	 * <p>Use <italic>FvTree#sort()</italic> method when you can.<br />
	 * FvTree keep expandable tree state.
	 */
	public function sort() : Void
	{
		var list : Array = getRoot().preorderEnumeration();
		var l : Number = list.length;
		
		for ( var i : Number = 0; i < l; i++ ) 
		{
			var node : FvAbstractTreeNode = list[ i ];
			if( !node.isLeaf() ) node.sortChildren( _sortingProcess, _sortingOptions );
		}
		
		nodeStructureChanged( FvAbstractTreeNode( getRoot() ) );
	}
	
	/**
	 * Sorts passed-in <code>node</code> childnodes.
	 */
	public function sortNode( node : FvAbstractTreeNode ) : Void
	{
		if( !node.isLeaf() ) 
		{
			node.sortChildren( _sortingProcess, _sortingOptions );
			nodeChanged( node );
		}
	}
	
    /**
      * Invoke this method after you've inserted some TreeNodes into
      * node.  childIndices should be the index of the new elements and
      * must be sorted in ascending order.
      */
    public function nodesWereInserted( node : TreeNode, childIndices : Array ) : Void
    {
        if( _listenerList != null && node != null && childIndices != null && childIndices.length > 0 )
        {
            var cCount : Number = childIndices.length;
            var newChildren : Array = new Array( cCount );

            for( var counter : Number = 0; counter < cCount; counter++ )
            {
                newChildren[counter] = node.getChildAt( childIndices[counter] );
            }
            _fireTreeNodeEvent( this, _getPathToRoot( node ), childIndices, newChildren, TREE_NODES_INSERTED_HANDLER );
        }
    }
    
    /**
      * Invoke this method after you've removed some TreeNodes from
      * node.  childIndices should be the index of the removed elements and
      * must be sorted in ascending order. And removedChildren should be
      * the array of the children objects that were removed.
      */
    public function nodesWereRemoved( node : TreeNode, childIndices : Array, removedChildren : Array ) : Void
    {
        if( node != null && childIndices != null )
        {
            _fireTreeNodeEvent( this, _getPathToRoot( node ), childIndices, removedChildren, TREE_NODES_REMOVED_HANDLER );
        }
    }
	
    /**
      * Invoke this method after you've changed how the children identified by
      * childIndicies are to be represented in the tree.
      */
    public function nodesChanged( node : TreeNode, childIndices : Array ) : Void
    {
        if( node != null )
        {
			if ( childIndices != null )
			{
				var cCount : Number = childIndices.length;
				
				if( cCount > 0 )
				{
			    	var cChildren : Array = new Array( cCount );
	 				
	 				for( var counter : Number = 0; counter < cCount; counter++ ) 
	 				{
						cChildren[counter] = node.getChildAt( childIndices[counter] );
	 				}
	 				
			    	_fireTreeNodeEvent( this, _getPathToRoot( node ), childIndices, cChildren, TREE_NODES_CHANGED_HANDLER );
				}
			}
			else if ( node == getRoot() )
			{
				_fireTreeNodeEvent( this, _getPathToRoot( node ), null, null, TREE_NODES_CHANGED_HANDLER );
		    }
        }
    }

    /**
      * Invoke this method if you've totally changed the children of
      * node and its childrens children.
      * 
      * <p>This will post a treeStructureChanged event.
      */
    public function nodeStructureChanged( node : TreeNode ) : Void
    {
        if( node != null )
        {
           _fireTreeNodeEvent( this, _getPathToRoot( node ), null, null, TREE_STRUCTURE_CHANGED_HANDLER );
        }
    }
	
	 /**
     * Addspassed-in <code>listener</code> listener for the TreeModelEvent posted after the tree changes.
     *
     * @param listener the listener to add
     */
    public function addTreeModelListener( listener : TreeModelListener ) : Void
    {
        _listenerList.push( listener );
    }

    /**
     * Removes passed-in <code>listener</code> listener.
     * 
     * @param listener the listener to remove
     */  
    public function removeTreeModelListener( listener : TreeModelListener ) : Void
    {
    	ArrayUtils.removeFromArray( _listenerList, listener );
    }
    
    /**
     * Returns string representation.
     */
    public function toString() : String
    {
    	return Stringifier.parse( this );
    }
    
    
    //-------------------------------------------------------------------------
    // Private implementation
    //-------------------------------------------------------------------------
	
    private function FvAbstractTreeModel( root : TreeNode, asc : Boolean )
    {
        if( asc == undefined ) asc = false;
        
        _rootNode = root;
        _asksAllowsChildren = asc;
        _listenerList = new Array();
        
        _autoSortingEnabled = false;
        _sortingProcess = FvAbstractTreeNode.defaultSortingProcess;
        _sortingOptions = Array.CASEINSENSITIVE;
    }
    
    private function _getPathToRoot( aNode : TreeNode, depth : Number ) : Array
    {
    	if( depth == undefined ) depth = 0;
		
		var retNodes : Array;
		
        if( aNode == null )
        {
            if( depth == 0 ) return null;
            else
            {
                retNodes = new Array(depth);
            }
        } 
        else
        {
            depth++;
            if( aNode == _rootNode)
            {
                retNodes = new Array(depth);
            }
            else
            {
                retNodes = _getPathToRoot( aNode.getParent(), depth );
            }
            retNodes[retNodes.length - depth] = aNode;
        }
        
        return retNodes;
    }
	
	private function _fireTreeNodeEvent( source : Object, path : Array, childIndices : Array, children : Array, handler : String )
	{
		var listeners : Array = _listenerList;
        var e : TreeModelEvent = null;
        
        for ( var i : Number = listeners.length-1; i >= 0; i-- )
        {
            if ( e == null )
            {
                e = new TreeModelEvent( source, path, childIndices, children );
            }
            
            var lis : TreeModelListener = TreeModelListener( listeners[i] );
            lis[ handler ]( e );
        }
	}
	
    private function _fireTreeStructureChanged( source : Object, path : TreePath ) : Void
    {
        var listeners:Array = _listenerList;
        var e:TreeModelEvent = null;
        
        for ( var i : Number = listeners.length-1; i >= 0; i-- )
        {
			if ( e == null )
			{
                e = new TreeModelEvent( source, path );
            }
            
            var lis : TreeModelListener = TreeModelListener( listeners[i] );
            lis.treeStructureChanged( e );
        }
    }
}